// pages/house/house.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 房源列表
    houseList: [
      {
        id: 1,
        title: '东湖小区 2室1厅',
        imageUrl: '/assets/images/house1.jpg',
        area: 89,
        type: '2室1厅',
        address: '武汉市武昌区珞瑜路',
        price: 2800,
        status: '待租',
        tags: ['精装修', '南北通透', '拎包入住']
      },
      {
        id: 2,
        title: '汉口花园 3室2厅',
        imageUrl: '/assets/images/house2.jpg',
        area: 120,
        type: '3室2厅',
        address: '武汉市江汉区解放大道',
        price: 3500,
        status: '已租',
        tags: []
      },
      {
        id: 3,
        title: '中南公寓 1室1厅',
        imageUrl: '/assets/images/house3.jpg',
        area: 55,
        type: '1室1厅',
        address: '武汉市洪山区珞喻路',
        price: 1800,
        status: '待租',
        tags: []
      },
      {
        id: 4,
        title: '江景豪园 4室2厅',
        imageUrl: '/assets/images/house4.jpg',
        area: 143,
        type: '4室2厅',
        address: '武汉市江岸区沿江大道',
        price: 4500,
        status: '已租',
        tags: []
      },
      // 新增3条房源数据
      {
        id: 7,
        title: '光谷新界 2室2厅',
        imageUrl: '/assets/images/house7.jpg',
        area: 95,
        type: '2室2厅',
        address: '武汉市洪山区光谷大道',
        price: 3000,
        status: '待租',
        tags: []
      },
      {
        id: 8,
        title: '水岸星城 3室1厅',
        imageUrl: '/assets/images/house8.jpg',
        area: 108,
        type: '3室1厅',
        address: '武汉市汉阳区龙阳大道',
        price: 3300,
        status: '待租',
        tags: []
      },
      {
        id: 9,
        title: '南湖花园 2室1厅',
        imageUrl: '/assets/images/house9.jpg',
        area: 85,
        type: '2室1厅',
        address: '武汉市武昌区南湖大道',
        price: 2600,
        status: '已租',
        tags: []
      }
    ],
    // 筛选条件
    filters: {
      area: '',
      price: '',
      type: '',
      status: ''
    },
    // 功能菜单
    menuList: [
      {
        id: 1,
        name: '在线看房',
        icon: '/assets/icons/view-house.png'
      },
      {
        id: 2,
        name: '在线签约',
        icon: '/assets/icons/sign.png'
      },
      {
        id: 3,
        name: '在线缴费',
        icon: '/assets/icons/payment.png'
      },
      {
        id: 4,
        name: '在线报修',
        icon: '/assets/icons/repair.png'
      }
    ],
    page: 1,
    loading: false,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.tab === 'repair') {
      // 直接跳转到报修页面
      wx.navigateTo({
        url: '/pages/house/repair/repair'
      })
    }
    this.getHouseList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 获取房源列表
  getHouseList() {
    // 应用筛选条件
    let filteredList = [...this.data.houseList];
    const filters = this.data.filters;
    
    if (filters.area) {
      filteredList = filteredList.filter(house => house.address.includes(filters.area));
    }
    if (filters.price) {
      // 假设价格筛选格式为: "0-3000", "3000-5000", "5000以上"
      const [min, max] = filters.price.split('-').map(Number);
      filteredList = filteredList.filter(house => {
        if (max) {
          return house.price >= min && house.price < max;
        }
        return house.price >= min;
      });
    }
    if (filters.type) {
      filteredList = filteredList.filter(house => house.type === filters.type);
    }
    if (filters.status) {
      filteredList = filteredList.filter(house => house.status === filters.status);
    }

    this.setData({
      houseList: filteredList,
      page: 1,
      hasMore: true
    });
  },

  // 筛选房源
  onFilterChange(e) {
    const { type, value } = e.currentTarget.dataset
    this.setData({
      [`filters.${type}`]: value
    })
    this.getHouseList()
  },

  // 点击房源
  onHouseClick(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/house/detail/detail?id=${id}`
    })
  },

  // 点击功能菜单
  onMenuClick(e) {
    const { id } = e.currentTarget.dataset
    switch(id) {
      case 1: // 在线看房
        wx.navigateTo({ 
          url: '/pages/house/view/online/online'
        })
        break
      case 2: // 在线签约
        // 检查是否有待签约的合同
        this.checkPendingContract()
        break
      case 3: // 在线缴费
        wx.navigateTo({ 
          url: '/pages/house/payment/payment' 
        })
        break
      case 4: // 在线报修
        wx.navigateTo({ 
          url: '/pages/house/repair/repair' 
        })
        break
    }
  },

  // 检查待签约合同
  checkPendingContract() {
    // TODO: 实际项目中应该调用API检查
    // 这里模拟一个待签约合同
    const pendingContract = {
      id: 'CT2024001',
      houseId: '1',
      status: '待签署'
    }

    if (pendingContract) {
      wx.navigateTo({
        url: `/pages/house/contract/sign?id=${pendingContract.id}`
      })
    } else {
      wx.showToast({
        title: '暂无待签约合同',
        icon: 'none'
      })
    }
  },

  // 加载更多
  loadMore() {
    if (this.data.loading || !this.data.hasMore) return
    
    this.setData({ loading: true })
    
    // 模拟加载更多数据
    setTimeout(() => {
      const newHouses = [
        {
          id: 5,
          title: '保利城 2室2厅',
          imageUrl: '/assets/images/house5.jpg',
          area: 98,
          type: '2室2厅',
          address: '武汉市江岸区后湖大道',
          price: 3200,
          status: '待租',
          tags: []
        },
        {
          id: 6,
          title: '绿地国际 3室1厅',
          imageUrl: '/assets/images/house6.jpg',
          area: 110,
          type: '3室1厅',
          address: '武汉市硚口区解放大道',
          price: 3800,
          status: '已租',
          tags: []
        }
      ]
      
      this.setData({
        houseList: this.data.houseList.concat(newHouses),
        page: this.data.page + 1,
        loading: false,
        hasMore: this.data.page < 3 // 示例中只加载到第3页
      })
    }, 1000)
  },

  // 搜索房源
  onSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  }
})